#coding: utf-8
require 'open-uri'
require 'json'
require 'hpricot'
def wtf(str)
  str = str.gsub(/[[:cntrl:]]/,'') 
  p str
  err= { :url => '', :text =>"void"}
  url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=#{str}"
  begin 
    buffer = open(URI.encode(url), "UserAgent" => "Ruby-Wget").read
  rescue 
    return err 
  end
  result = JSON.parse buffer
  x = result['responseData']['results'][0]  
  return err if x == nil
  {
    :url => x['unescapedUrl'],
    :text => Hpricot(x['content']).inner_text
  }
end
